package com.ruoyi.common.utils;

import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson2.JSON;

/**
 * 规格组工具类
 * 用于处理specNameGroup和specValueGroup的双斜杠处理逻辑
 *
 * @author ruoyi
 * @date 2025-01-27
 */
public class SpecGroupUtils {

    /**
     * 处理规格名称组，去除双斜杠并转换为List
     *
     * @param specNameGroup 规格名称组字符串
     * @return 处理后的规格名称列表
     */
    public static List<String> processSpecNameGroup(String specNameGroup) {
        if (specNameGroup == null) {
            return new ArrayList<>();
        }

        try {
            String specNameStr = specNameGroup.replace("\\", "");
            if (specNameStr.startsWith("[") && specNameStr.endsWith("]")) {
                specNameStr = specNameStr.substring(1, specNameStr.length() - 1);
                String[] names = specNameStr.split(",");
                List<String> nameList = new ArrayList<>();
                for (String name : names) {
                    nameList.add(name.trim().replace("\"", ""));
                }
                return nameList;
            }
        } catch (Exception e) {
            // 记录日志或处理异常
            return new ArrayList<>();
        }
        return new ArrayList<>();
    }

    /**
     * 处理规格值组，去除双斜杠并转换为List
     *
     * @param specValueGroup 规格值组字符串
     * @return 处理后的规格值列表
     */
    public static List<String> processSpecValueGroup(String specValueGroup) {
        if (specValueGroup == null) {
            return new ArrayList<>();
        }

        try {
            String specValueStr = specValueGroup.replace("\\", "");
            if (specValueStr.startsWith("[") && specValueStr.endsWith("]")) {
                specValueStr = specValueStr.substring(1, specValueStr.length() - 1);
                String[] values = specValueStr.split(",");
                List<String> valueList = new ArrayList<>();
                for (String value : values) {
                    valueList.add(value.trim().replace("\"", ""));
                }
                return valueList;
            }
        } catch (Exception e) {
            // 记录日志或处理异常
            return new ArrayList<>();
        }
        return new ArrayList<>();
    }

    /**
     * 批量处理规格组数据
     *
     * @param specNameGroup  规格名称组字符串
     * @param specValueGroup 规格值组字符串
     * @return 包含处理后的规格名称列表和规格值列表的数组，索引0为名称列表，索引1为值列表
     */
    public static List<String>[] processSpecGroups(String specNameGroup, String specValueGroup) {
        List<String> nameList = processSpecNameGroup(specNameGroup);
        List<String> valueList = processSpecValueGroup(specValueGroup);

        @SuppressWarnings("unchecked")
        List<String>[] result = new List[2];
        result[0] = nameList;
        result[1] = valueList;

        return result;
    }

    /**
     * 验证规格组字符串格式是否正确
     *
     * @param specGroup 规格组字符串
     * @return 是否为有效的规格组格式
     */
    public static boolean isValidSpecGroupFormat(String specGroup) {
        if (specGroup == null) {
            return false;
        }

        String processed = specGroup.replace("\\", "");
        return processed.startsWith("[") && processed.endsWith("]");
    }

    /**
     * 处理规格名称组，去除双斜杠并转换为List，然后转换为JSON字符串
     *
     * @param specNameGroup 规格名称组字符串
     * @return 处理后的规格名称组JSON字符串
     */
    public static String processSpecNameGroupToString(String specNameGroup) {
        List<String> nameList = processSpecNameGroup(specNameGroup);
        return JSON.toJSONString(nameList);
    }

    /**
     * 处理规格值组，去除双斜杠并转换为List，然后转换为JSON字符串
     *
     * @param specValueGroup 规格值组字符串
     * @return 处理后的规格值组JSON字符串
     */
    public static String processSpecValueGroupToString(String specValueGroup) {
        List<String> valueList = processSpecValueGroup(specValueGroup);
        return JSON.toJSONString(valueList);
    }
}